/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-12-16 02:42:22
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2023-12-16 19:59:08
 * @FilePath: \koa-server\src\service\menu.service.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const Menu = require('../model/menu.model')
const { Op } = require("sequelize")
class menuService {
  async getMenu(params){
    const json = await Menu.findAll({
      where:{ [Op.or]:[{pId:params.id}] }
    })
    return json
  }
  //查询所有
  async allSearch(){
    const json = await Menu.findAll()
    return json
  }
  //分页查询
  async search(params){
    let obj = Object.assign({},params)
    delete obj.pageNo
    delete obj.pageSize
    const { count, rows } = await Menu.findAndCountAll(
      {
        limit:params.pageSize,
        offset:(params.pageNo -1) * params.pageSize,
        where:{...obj} 
      },
    )
    return {
      rows,
      count
    }
  }
  //新增
  async insertItem(params){
    const res = await Menu.create(params)
    return res
  }
  //修改
  async updateItem(params){
    const res = await Menu.update(
      {...params},
      { where:{id:params.id} }
    )
    return res
  }
  //删除
  async deleteOne(params){
    const res = await Menu.destroy({ where:{ id:params.id } })
    return res
  }
  //查询一条信息
  async getOne(params){
    const res = await Menu.findOne({
      where:{ id:params.id }
    })
    return res
  }
}

module.exports = new menuService()